class CreateRecommendations < ActiveRecord::Migration
  def self.up
    create_table :recommendations do |t|
      t.integer :entry_id
      t.integer :dest_entry_id
      t.integer :rank
      t.decimal :relevance, :precision => 18, :scale => 17, :default => 0
      t.integer :clicks, :default => 0
      t.integer :avg_time_at_dest, :default => 60      
    end

    execute "UPDATE recommendations SET avg_time_at_dest = 60"

    add_index :recommendations, :entry_id
    execute "ALTER TABLE recommendations ADD CONSTRAINT recommendations_entry_id_fkey FOREIGN KEY (entry_id) REFERENCES entries (id) ON DELETE CASCADE ON UPDATE CASCADE"

    add_index :recommendations, :dest_entry_id
    execute "ALTER TABLE recommendations ADD CONSTRAINT recommendations_dest_entry_id_fkey FOREIGN KEY (dest_entry_id) REFERENCES entries (id) ON DELETE CASCADE ON UPDATE CASCADE"
  end

  def self.down
    execute "ALTER TABLE recommendations DROP CONSTRAINT recommendations_entry_id_fkey"
    remove_index :recommendations, :entry_id
    execute "ALTER TABLE recommendations DROP CONSTRAINT recommendations_dest_entry_id_fkey"
    remove_index :recommendations, :dest_entry_id
    drop_table :recommendations
  end
end
